package com.netty.kkk.controller;

import com.netty.kkk.factory.ResultFactory;
import com.netty.kkk.service.ChatService;
import com.netty.kkk.service.UserService;
import com.netty.kkk.vo.ChatInfo;
import com.netty.kkk.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@RestController
public class ChatCrl {
    @Autowired
    private UserService userService;
    @Autowired
    private ChatService chatService;

    @CrossOrigin
    @RequestMapping(value = "/api/chat/deleteChat", method = RequestMethod.GET, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result deleteChat(@PathParam("chatId")String chatId, @PathParam("userName")String userName){
        chatService.deleteChat(chatId);
        System.out.println("删除了群组"+chatId);
        WebsocketGroupCrl websocketGroupCrl = new WebsocketGroupCrl();
        websocketGroupCrl.sendMsg(chatId, userName,"删除了改群聊", websocketGroupCrl.DeleteChat_Type);
        return ResultFactory.buildSuccessResult("删除群组成功");
    }

    @CrossOrigin
    @RequestMapping(value = "/api/chat/quitChat", method = RequestMethod.GET, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result quitChat(@PathParam("chatId")String chatId, @PathParam("userName")String userName){
        chatService.quitChat(chatId, userName);
        System.out.println(userName+"退出了群组"+chatId);
        WebsocketGroupCrl websocketGroupCrl = new WebsocketGroupCrl();
        websocketGroupCrl.sendMsg(chatId, userName,"退出了该群聊", websocketGroupCrl.Quit_Type);
        return ResultFactory.buildSuccessResult("已退出群聊");
    }

    @CrossOrigin
    @RequestMapping(value = "/api/chat/changeChatName", method = RequestMethod.GET, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result changeChatName(@PathParam("chatId")String chatId, @PathParam("userName")String userName, @PathParam("chatName")String chatName){
        chatService.changeChatName(chatId, chatName);
        System.out.println(userName+"修改了群聊名"+chatId+"为"+chatName);
        WebsocketGroupCrl websocketGroupCrl = new WebsocketGroupCrl();
        websocketGroupCrl.sendMsg(chatId, userName, chatName, websocketGroupCrl.ChangeChatName_Type);
        return ResultFactory.buildSuccessResult("群聊名修改成功");
    }

    @CrossOrigin
    @RequestMapping(value = "/api/chat/createNewChat", method = RequestMethod.POST, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result createNewChat(@PathParam("memberList") String memberList, @PathParam("chatInfo") String chatName){
        List<String> list = new ArrayList<>();
        String arr[] = memberList.replace("&","").split("memberList=");
        for(String temp : arr){
            if (!temp.trim().equals("")){
                list.add(temp.trim());
            }
        }
        ChatInfo chatInfo = new ChatInfo();
        chatInfo.setChatName(chatName);
        chatService.createNewChat(chatInfo);
        int chatId = chatInfo.getChatId();
        chatService.insertMembers(chatId, list);
        System.out.println("创建了新群聊:"+chatInfo.getChatName()+"---chatId:"+chatId);
        WebsocketUserCrl websocketUserCrl = new WebsocketUserCrl();
        websocketUserCrl.createNewGroupChat(list,chatInfo);
        return ResultFactory.buildSuccessResult("创建群聊成功");
    }
}
